/*
8. Flush Straight
7. Four of a kind
6. Full House
5. Flush
4. Straight
3. 3 of a kind
2. 2 Pairs
1. Pair
0. High card
*/


/**
 * Hand Value.
 */
function getHandValue(hands) {
  hands.sort(function(a, b) {return b.cardValue - a.cardValue});
  var handsRank = handsToCardValueTab(hands);
  if (isStraightFlush(hands))
    return [8, hands[4].cardValue];
  else if (countKinds(hands, 4))
    return [7, countKinds(hands, 4), countKinds(hands, 1)];
  else if (countKinds(hands, 2) !== false && countKinds(hands, 3))
    return [6, countKinds(hands, 3), countKinds(hands, 2)];
  else if (isColor(hands))
    return [5, handsRank];
  else if (isStraight(hands))
    return [4, hands[4].cardValue];
  else if (countKinds(hands, 3))
    return [3, countKinds(hands, 3), handsRank];
  else if (isTwoPair(hands))
    return [2, isTwoPair(hands), handsRank];
  else if (countKinds(hands, 2) !== false)
    return [1, countKinds(hands, 2), handsRank];
  else
    return [0, handsRank];
}

/**
 * Straight Flush.
 */
function handsToCardValueTab(hands) {
  var tabCardValue = [];
  for (var i = 0; i < hands.length; i++) {
    tabCardValue[i] = hands[i].cardValue;
  }
  return tabCardValue;
}


/**
 * Straight Flush.
 */
function isStraightFlush(hands) {
  return isColor(hands) && isStraight(hands);
}


/**
 * Color.
 */
function isColor(hands) {
  var handColor = hands[0].color; 
  for (var i = 1; i < hands.length; i++) {
    if (handColor != hands[i].color) {
      return false;
    }
  }
  return true;
}



/**
 * Straight.
 */
function isStraight(hands) {
  var handsRank = handsToCardValueTab(hands);
  var special = [12, 3, 2, 1, 0];
  if (special.toString() == handsRank.toString()) {
    return true;
  }
  for (var i = 0; i < hands.length - 1; i++) {
    if (hands[i].cardValue != hands[i + 1].cardValue + 1) {
      return false;
    }
  }
  return true;
}



/**
 * Counts Kinds.
 */
function countKinds(hands, n) {
  var counts = [];
  for (var i = 0; i < cardsValue.length; i++) {
  	counts.push(0);
  }
  for (var i = 0; i < hands.length; i++) {
    counts[hands[i].cardValue]++;
  }
  for (var i = 0; i < hands.length; i++) {
    if (counts[hands[i].cardValue] == n)
      return hands[i].cardValue;
  }
  return false;
}



/**
 * Two Pairs.
 */
function isTwoPair(hands) {
  var highPair = countKinds(hands, 2)
  if (highPair === false) return false;
  hands.sort(function(a, b) {return a.cardValue - b.cardValue});
  var lowPair = countKinds(hands, 2);
  if (lowPair == highPair)
    return false;
  else
    return [highPair, lowPair ];
}


/**
 * Two Pairs.
 */
function isTwoPair(hands) {
  var highPair = countKinds(hands, 2)
  if (highPair === false) return false;
  hands.sort(function(a, b) {return a.cardValue + b.cardValue});
  var lowPair = countKinds(hands, 2);
  if (lowPair == highPair)
    return false;
  else
    return [highPair, lowPair ];
}


/**
 * Compares two hands.
 */
function comparatorTwoHands(hands1, hands2) {
  var valueHand1 = getHandValue(hands1);
  var valueHand2 = getHandValue(hands2);
  for (var i = 0; i < valueHand1.length; i++) {
    if (valueHand1[i] > valueHand2[i]) {
	  return 1;
	} else if (valueHand1[i] < valueHand2[i]) {
	  return -1;
	}
  }
  return 0;
}

/**
 * Returns the best hand between 2 hands.
 */
function returnBestHand(hands1, hands2) {
  var valueHand1 = getHandValue(hands1);
  var valueHand2 = getHandValue(hands2);
  for (var i = 0; i < valueHand1.length; i++) {
    if (valueHand1[i] > valueHand2[i]) {
	  return hands1;
	} else if (valueHand1[i] < valueHand2[i]) {
	  return hands2;
	}
  }
  return hands1;
}

/**
 * Returns the best value between 2 hands.
 */
function returnBestValueForTwoHandsValues(valueHand1, valueHand2) {
  for (var i = 0; i < valueHand1.length; i++) {
    if (valueHand1[i] > valueHand2[i]) {
	  return valueHand1;
	} else if (valueHand1[i] < valueHand2[i]) {
	  return valueHand2;
	}
  }
  return valueHand1;
}

/**
 * 
 */
function compareTwoHands(hands1, hands2) {
  var diff = comparatorTwoHands(hands1, hands2);
  if (diff > 0) {
    return 'hands1 is better than hands2';
  } else if (diff < 0) {
    return 'hands1 is lower than hands2';
  } else{
    return 'hands1 = hands 2';
  }
}


















